import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path:"/Login",
    name:"/login",
    component:()=>import('../views/Login.vue')
  },
  {
    path:"/home",
    name:"/home",
    component:()=>import('../views/home.vue'),
    children:[
      {
        path:"/users",
        name:"/users",
        component:()=>import('../components/users/users.vue')
      },
      {
        path:"/rights",
        name:"/rights",
        component:()=>import('../components/rights/rights.vue')
      },
      {
        path:"/roles",
        name:"/roles",
        component:()=>import('../components/rights/roles.vue')
      },
      {
        path:"/goods",
        name:"/goods",
        component:()=>import('../components/goods/goods.vue')
      },
      {
        path:"/goods/add",
        name:"/add",
        component:()=>import('../components/goods/add.vue')
      },
      {
        path:"/params",
        name:"/params",
        component:()=>import('../components/goods/params.vue')
      },
      {
        path:"/categories",
        name:"/categories",
        component:()=>import('../components/goods/categories.vue')
      },
      {
        path:"/orders",
        name:"/orders",
        component:()=>import('../components/orders/orders.vue')
      },
      {
        path:"/reports",
        name:"/reports",
        component:()=>import('../components/reports/reports.vue')
      },
    ]
  },
  
  {
    path:"/",
    redirect:localStorage.getItem('CurrentRoute')?localStorage.getItem('CurrentRoute'):'/home'
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,form,next)=>{
  let token=localStorage.getItem('Token')
  // console.log(token);
  if(to.path=='/Login'){
    next()
  }else{
    if(token){
      next()
    }else{
      next('/Login')
    }
  }
 

})


export default router
